package linkedList;

/**
 * @Description TODO
 * @Author zrj
 * @Date 2020/8/3 12:34
 * @Version 1.0
 */
public class LinkedList<E> {

    private Node head;
    private Node next;
    private Node prev;
    private int size;

    public LinkedList() {
        this.head = null;
        this.size = 0;
    }

    public void addFirst(E e) {
        //head = new Node(e, head);
        Node node = new Node(e);
        node.next = head;
        this.head = node;
        size++;

    }

    public void add(int index, E e) {

        if (index == 0) {
            addFirst(e);
        }else {
            Node prev = head;
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }
            //prev.next = new Node(e, prev.next);
            Node node = new Node(e);
            node.next = prev.next;
            prev.next = node;
        }

        size ++;
    }

    public void addLast(E e) {
        add(size, e);
    }


    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    ", next=" + next +
                    '}';
        }
    }
}
